import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import store from "@/store";

const routes = [
  // {
  //   path: '/',
  //   name: 'Mange',
  //   redirect: '/home',
  //   component: () => import('../views/ManageView.vue'),

  //   children: [{
  //       path: 'home',
  //       name: '首页',
  //       component: () => import('../views/HomeView.vue'),
  //     }, {
  //       path: 'person',
  //       name: '个人中心',
  //       component: () => import('../views/Home/PersonView.vue'),
  //     },
  //     {
  //       path: 'file',
  //       name: '文件管理',
  //       component: () => import('../views/FileView.vue'),
  //     },

  //     // 疫情数据可视化模块
  //     {
  //       path: 'user',
  //       name: '用户管理',
  //       component: () => import('../views/Visualization/CommonView.vue'),

  //     },
  //     {
  //       path: 'china',
  //       name: '中国疫情图',
  //       component: () => import('../views/Visualization/ChinaView.vue'),
  //     },
  //     {
  //       path: 'global',
  //       name: '世界疫情图',
  //       component: () => import('../views/Visualization/GlobalView.vue'),
  //     },

  //     // 疫情数据管理模块
  //     {
  //       path: 'chinadata',
  //       name: '中国疫情数据',
  //       component: () => import('../views/CovidData/ChinaDataManger.vue'),
  //     }, {
  //       path: 'globaldata',
  //       name: '世界疫情数据',
  //       component: () => import('../views/CovidData/GlobalDataManger.vue'),
  //     },

  //     //校园疫情管理模块

  //      {
  //       path: 'heath',
  //       name: '健康打卡',
  //       component: () => import('../views/Campus/HeathClockDataManger.vue'),
  //     },
  //     //系统管理模块
  //     {
  //       path:'menu',
  //       name:'菜单管理',
  //       component: () => import('../views/System/MenuView.vue'),
  //     },
  //     {
  //       path:'role',
  //       name:'角色管理',
  //       component: () => import('../views/System/RoleView.vue'),
  //     },



  //   ]
  // },
  // 登录页面
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
  },
  {
    path: '/register',
    name: 'Register',

    component: () => import('../views/RegisterView.vue'),
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404View.vue'),
  },

  
  // {
  //   path: '/:pathMatch(.*)',
  //   redirect: '/404'

  // },
  // 

  // 用户前台
  {
    path: '/front',
    name: 'front',
    component: () => import('../views/UserHome/Front.vue'),
    children: [{
      path: 'userhourse',
      name: '用户首页',
      component: () => import('../views/UserHome/Home.vue'),
    },
    {
      path: 'userperson',
      name: '用户个人中心',
      component: () => import('../views/UserHome/PersonView.vue'),
    },
    {
      path: 'hesuanresult',
      name: '用户核酸结果',
      component: () => import('../views/UserHome/HesuanView.vue'),
    },
    {
      path: 'vaccinesituation',
      name: '疫苗接种情况',
      component: () => import('../views/UserHome/VaccineView.vue'),
    },
    {
      path: 'myheathclock',
      name: '健康打卡',
      component: () => import('../views/UserHome/HeathclockView.vue'),
    },
    {
      path: 'recording',
      name: '打卡记录',
      component: () => import('../components/usertable/Recording.vue'),
    },

    {
      path: 'reviserecording',
      name: '查询记录',
      component: () => import('../components/usertable/ReviseRecording.vue'),
    },
    {
      path: 'personal',
      name: '我的个人中心',
      component: () => import('../components/usertable/PersonalView.vue'),
    },
  ]

  },


]

// 后台模块
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//注意刷新页面会导致页面重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem('menus');
  if (storeMenus) {
    //拼装动态路由
    const manageRoute = {
      path: '/',
      name: 'mange',
      redirect: '/home',
      component: () => import('../views/ManageView.vue'),
      children: [{
        path: 'person',
        name: '个人中心',
        component: () => import('../views/Home/PersonView.vue'),
      }]
    }
    //解析
    const menus = JSON.parse(storeMenus)
    menus.forEach(item => {
      if (item.href) { //当且仅当path不为空的时候才去设置路由
        console.log(typeof (item.pageFile));
        if (item.pageFile === null) {
          //子路由去掉斜杆
          let itemMenu = {
            path: item.href.replace("/", ""),
            name: item.title,
            component: () => import('../views/' + item.pagePath + '.vue'),

          }
          manageRoute.children.push(itemMenu)
        } else {
          let itemMenu = {
            path: item.href.replace("/", ""),
            name: item.title,
            component: () => import('../views/' + item.pageFile + '/' + item.pagePath + '.vue'),

          }
          manageRoute.children.push(itemMenu)
        }


      } else if (item.children.length) {
        item.children.forEach(item => {
          if (item.href) {
            if (item.pageFile === null) {
              let itemMenu = {
                path: item.href.replace("/", ""),
                name: item.title,
                component: () => import('../views/' + item.pagePath + '.vue'),
              }
              manageRoute.children.push(itemMenu)
            } else {
              let itemMenu = {
                path: item.href.replace("/", ""),
                name: item.title,
                component: () => import('../views/' + item.pageFile + '/' + item.pagePath + '.vue'),

              }
              manageRoute.children.push(itemMenu)
            }
          }
        })
      }
    })
    //动态添加到现在的路由对象中去
    router.addRoute(manageRoute)
  }
}
//重置我就再set刷新一次路由
setRoutes()

//路由守卫
router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name) // 设置当前的路由名称，为了在Header组件中去使用
  store.commit("setPath") // 触发store的数据更新
  //未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menus");
    if (storeMenus) { //校验当前用户是否登录
      next("/404")
    } else {
      next("/login")
    }
  }
  //其他的情况都放行
  next() // 放行路由
})
export default router